package com.example.mobifin_busticket.views.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import com.example.mobifin_busticket.AppData;
import com.example.mobifin_busticket.R;
import com.example.mobifin_busticket.models.BusModel;
import com.example.mobifin_busticket.models.SourceModel;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Filter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class FilterBusAdapter extends BaseAdapter implements OnClickListener {
	class ViewHolder {

		TextView tvTravelName;
		CheckBox chkbox;
		LinearLayout llFilter;
	}

	private Context mContext;
	private ArrayList<String> listval;
	static ArrayList<String> finalselectedList = null;
	private ArrayList<String> selectListObj;
	private ArrayList<String> listvaltemp = null;

	LayoutInflater vi;
	CheckBox chkselectall = null;
	ViewHolder holder = new ViewHolder();
	boolean[] itemChecked;

	public FilterBusAdapter(Context ctx, List<String> listvalue, CheckBox chkbox) {
		this.mContext = ctx;
		this.listval = (ArrayList<String>) listvalue;
		this.chkselectall = chkbox;
		this.listvaltemp = new ArrayList<String>();
		this.listvaltemp.addAll(listval);
		this.finalselectedList = new ArrayList<String>();
		itemChecked = new boolean[listvalue.size()];
		vi = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listval.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return listval.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub

		System.out.println("In Get View==>position:" + position);

		View v = convertView;

		if (v == null) {

			holder = new ViewHolder();
			v = vi.inflate(R.layout.row_filter_bus, null);
			holder.tvTravelName = (TextView) v.findViewById(R.id.txt_list);
			holder.chkbox = (CheckBox) v.findViewById(R.id.chk_item);
			holder.llFilter = (LinearLayout) v.findViewById(R.id.llFilter);

			v.setTag(holder);

		} else {
			holder = (ViewHolder) v.getTag();
		}

		holder.chkbox.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				if (((CheckBox) v).isChecked()) {
					itemChecked[position] = true;
					finalselectedList.add(listval.get(position));
					System.out.println("select value:" + finalselectedList);

				} else {
					itemChecked[position] = false;
					holder.chkbox.setChecked(false);
					chkselectall.setChecked(false);
					v.setBackgroundResource(R.drawable.filter_listselector);
					holder.chkbox.setButtonDrawable(R.drawable.ckb_bg_selector);
					finalselectedList.remove(listval.get(position));
					System.out.println("select value:" + finalselectedList);

				}

				System.out.println("main list size:" + listval.size());
				System.out.println("selectes list size:"
						+ finalselectedList.size());

			}

		});

		if (finalselectedList.contains(listval.get(position))) {
			holder.chkbox.setChecked(true);
			System.out.println("seected list:" + finalselectedList);

		} else {
			holder.chkbox.setChecked(false);
		}
		holder.tvTravelName.setText(listval.get(position));
		holder.chkbox.setId(position);
		return v;
	}

	public void selectedAll() {

		for (int i = 0; i < itemChecked.length; i++) {

			itemChecked[i] = true;
			holder.chkbox.setChecked(true);
			finalselectedList.add(listval.get(i));
			System.out.println("select value on select all:"
					+ finalselectedList);
		}

		notifyDataSetChanged();

	}

	public void uncheckedAll() {

		for (int i = 0; i < itemChecked.length; i++) {
			itemChecked[i] = false;
			holder.chkbox.setChecked(false);
			finalselectedList.remove(listval.get(i));
			System.out.println("new value on uncheched all:"
					+ finalselectedList);

		}
		notifyDataSetChanged();

	}

	public void mSelectAllHandle() {

		if (finalselectedList.size() < listval.size()) {
			chkselectall.setChecked(false);
		} else if (finalselectedList.size() == listval.size()) {
			chkselectall.setChecked(true);
		} else {
			chkselectall.setChecked(false);
		}
	}

	public void ItemFilter(String charText) {

		String filterString = charText.toLowerCase(Locale.getDefault());
		listval.clear();

		if (filterString.length() == 0) {
			listval.addAll(listvaltemp);
		} else {
			for (String tempsearch : listvaltemp) {
				if (tempsearch.toLowerCase(Locale.getDefault()).trim()
						.startsWith(filterString)) {
					listval.add(tempsearch);
				}
			}
		}

		notifyDataSetChanged();
	}

	public List<String> getFilterSelectedList() {
		return finalselectedList;

	}

	public void setFilterSelectedList() {
		this.finalselectedList = new ArrayList<String>(finalselectedList);

	}

	public static void setSelectTravelList(List<String> selectListFilter) {
		AppData.selectListFilterTravels = finalselectedList;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}
}
